package com.chaoyi.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.chaoyi.model.User;
import com.chaoyi.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	@Resource
	private UserService userService;
	
	/**
	 * 列表展示
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ModelAndView mod = new ModelAndView("/user/userList");
		return mod;
	}
	
	/**
	 * 新增跳转
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/addUser_plus")
	public ModelAndView addUser_plus(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ModelAndView mod = new ModelAndView("/user/addUser");
		return mod;
	}
	
	@ResponseBody
	@RequestMapping("/userList")
	public JSONObject userList(HttpServletRequest request, HttpServletResponse response,User user){
		String offset = request.getParameter("offset");
		String limit = request.getParameter("limit");
		List<User> userList = userService.findUserList(offset,limit,user);
//		int userNumber = userService.selectUsernumber();
		JSONObject json = new JSONObject();
		json.put("rows", userList);
		json.put("total", userList.size());
		return json;
	}
	
	@ResponseBody
	@RequestMapping("/addUser")
	public JSONObject addUser(HttpServletRequest request, HttpServletResponse response ,User user){
		JSONObject json = new JSONObject();
		int userNumber = userService.findUserNameNum(user.getUserName(),0);
		if(userNumber !=0){
			json.put("missage", "userNameRepeat");
			return json;
		}
		int mark = userService.addUser(user);
		if(mark==1){
			json.put("missage", "success");
		}
		return json;
	}
	
	@RequestMapping("/alertUser")
	public ModelAndView alertUser(HttpServletRequest request, HttpServletResponse response ,int id){
		User user = userService.findUserOne(id);
		ModelAndView mod = new ModelAndView("user/alertUser");
		mod.addObject("obj",user);
		return mod;
	}
	
	//修改保存
	@ResponseBody
	@RequestMapping("/updateUser")
	public JSONObject updateUser(HttpServletRequest request, HttpServletResponse response,User user){
		JSONObject json = new JSONObject();
		int userNumber = userService.updateUser(user);
		if(userNumber !=0){
			json.put("missage", "success");
			return json;
		}
		json.put("missage", "error");
		return json;
	}
	
	//修改保存
	@ResponseBody
	@RequestMapping("/deleteUser")
	public JSONObject deleteUser(HttpServletRequest request, HttpServletResponse response,int id){
		JSONObject json = new JSONObject();
		int userNumber = userService.deleteUser(id);
		if(userNumber !=0){
			json.put("missage", "success");
			return json;
		}
		json.put("missage", "error");
		return json;
	}
	
	
	
	//校验用户名是否重复
	@ResponseBody
	@RequestMapping("/checkUserName")
	public JSONObject checkUserName(HttpServletRequest request, HttpServletResponse response,String userName,int id){
		JSONObject json = new JSONObject();
		int userNumber = userService.findUserNameNum(userName,id);
		if(userNumber !=0){
			json.put("valid", false);
			return json;
		}
		json.put("valid", true);
		return json;
	}
	
//	//商务下拉菜单
//	@ResponseBody
//	@RequestMapping("/findBusiness")
//	public JSONObject findBusiness(HttpServletRequest request, HttpServletResponse response){
//		JSONObject json = new JSONObject();
//		User user = (User) request.getSession().getAttribute(SessionConfig.USER);
//		if(user.getRoleId() != 2){
//			List<User> userList = userService.findBusiness();
//			json.put("userList", userList);
//		}else{
//			List<User> userList = new ArrayList<User>();
//			userList.add(user);
//			json.put("userList", userList);
//		}
//		return json;
//	}

}
